/*Joseph A. Coll
RCV AP Main Methods
7/23/2025*/

*Load data
use "G:\My Drive\Backup 08202022\Working Papers\RCV and AP\Data and Methods\RCV AP Data Cleaned.dta", clear
cd "G:\My Drive\Backup 08202022\Working Papers\RCV and AP\Tables and Figures RCV AP"

********************************************************************************	
********************************************************************************
********************************************************************************
**#Main Analyses and Figures
********************************************************************************
********************************************************************************
********************************************************************************	

********************************************************************************
*Does Treatment Affect Affective Polarization
********************************************************************************
*FT
	*Affective polarization 
	reg ap_ft_change i.treatment
	estimates store ap_ft_change
	margins, at(treatment=(1(1)3)) post 
	coefplot, vertical ///
	ciopts(recast(rcap) msize(small)) ///
	scheme(plotplain) ///
	xlabel(1 `" "Plurality" "Comp."' 2 `" "RCV" "Comp."'  3 `" "RCV" "Comp." "& Endorse"' ) ///
	xscale(r(1.25 2.25)) ///
	title("", size(medium)) ///
	ytitle("Affective Polarization {&Delta} ") ///
	yline(0, lpattern(longdash) lcolor(black%60)) ///
	yscale(r(-.035 .035)) ///
	ylabel(-.03(.01).03) ///
	mlabel(cond(@pval>.05, string(@b,"%9.3f") + "", ///
		cond(@pval<.05, string(@b,"%9.3f") + "***", ///
		cond(@pval<.01, string(@b,"%9.3f") + "**", ///
		cond(@pval<.001, string(@b,"%9.3f") + "*", ///
		string(@pval,"%9.3fThe")))))) ///
	name(ap_ft_change, replace) 
	graph export ap_ft_change.png, replace
	
	*In group
	reg ap_ft_ip_change  i.treatment
	estimates store ap_ft_ip_change
	margins, at(treatment=(1(1)3)) post
	coefplot, vertical ///
	ciopts(recast(rcap) msize(small)) ///
	scheme(plotplain) ///
	xlabel(1 `" "Plurality" "Comp."' 2 `" "RCV" "Comp."'  3 `" "RCV" "Comp." "& Endorse"' ) ///
	xscale(r(1.25 2.25)) ///
	title("", size(medium)) ///
	ytitle("In-Group Affect {&Delta}") ///
	yline(0, lpattern(longdash) lcolor(black%60)) ///
	yscale(r(-.035 .035)) ///
	ylabel(-.03(.01).03) ///
	mlabel(cond(@pval>.05, string(@b,"%9.3f") + "", ///
		cond(@pval<.05, string(@b,"%9.3f") + "***", ///
		cond(@pval<.01, string(@b,"%9.3f") + "**", ///
		cond(@pval<.001, string(@b,"%9.3f") + "*", ///
		string(@pval,"%9.3fThe")))))) ///
	name(ap_ft_ip_change, replace) 
	graph export ap_ft_ip_change.png, replace	
	
	*Outgroup
	reg ap_ft_op_change  i.treatment 
	estimates store ap_ft_op_change
	margins, at(treatment=(1(1)3)) post
	coefplot, vertical ///
	ciopts(recast(rcap) msize(small)) ///
	scheme(plotplain) ///
	xlabel(1 `" "Plurality" "Comp."' 2 `" "RCV" "Comp."'  3 `" "RCV" "Comp." "& Endorse"' ) ///
	xscale(r(1.25 2.25)) ///
	title("", size(medium)) ///
	ytitle("Out-Group Affect {&Delta}") ///
	yscale(r(-.035 .035)) ///
	ylabel(-.03(.01).03) ///
	yline(0, lpattern(longdash) lcolor(black%60)) ///
	mlabel(cond(@pval>.05, string(@b,"%9.3f") + "", ///
		cond(@pval<.05, string(@b,"%9.3f") + "***", ///
		cond(@pval<.01, string(@b,"%9.3f") + "**", ///
		cond(@pval<.001, string(@b,"%9.3f") + "*", ///
		string(@pval,"%9.3fThe")))))) ///
	name(ap_ft_op_change, replace) 
	graph export ap_ft_op_change.png, replace	
	
	coefplot ap_ft_change ap_ft_ip_change ap_ft_op_change , ///
	drop(_cons) xline(0) ///
	xscale(r(-.015 .015)) ///
	xlabel(-.01(.01).01) ///
	scheme(plotplain) ///
	legend(order(2 "In- minus Out-Ideology" 4 "In-Ideology" 6 "Out-Ideology") row(1) pos(6)) ///
	title("Feeling Thermometer") ///
	mlabel(cond(@pval>.05, string(@b,"%9.3f") + "", ///
	cond(@pval<.001, string(@b,"%9.3f") + "***", ///
	cond(@pval<.01, string(@b,"%9.3f") + "**", ///
	cond(@pval<.05, string(@b,"%9.3f") + "*", ///
	string(@pval,"%9.3f")))))) ///
	mlabpos(12) ///
	mlabgap(2.5) ///
	name(coef_ft, replace)

	graph combine ap_ft_change ap_ft_ip_change ap_ft_op_change, ///
	ycommon xcommon ///
	rows(1) ///
	name(ft_combined, replace)
	graph export ft_combined.png, replace 

*Traits
	*Affective polarization
	reg ap_traits_change  i.treatment
	estimates store ap_traits_change
	margins, at(treatment=(1(1)3)) post
	coefplot, vertical ///
	ciopts(recast(rcap) msize(small)) ///
	scheme(plotplain) ///
	xlabel(1 `" "Plurality" "Comp."' 2 `" "RCV" "Comp."'  3 `" "RCV" "Comp." "& Endorse"' ) ///
	xscale(r(1.25 2.25)) ///
	title("", size(medium)) ///
	ytitle("Affective Polarization {&Delta} ") ///
	yscale(r(-.04 .04)) ///
	ylabel(-.03(.01).03) ///
	yline(0, lpattern(longdash) lcolor(black%60)) ///
	mlabel(cond(@pval>.05, string(@b,"%9.3f") + "", ///
		cond(@pval<.05, string(@b,"%9.3f") + "***", ///
		cond(@pval<.01, string(@b,"%9.3f") + "**", ///
		cond(@pval<.001, string(@b,"%9.3f") + "*", ///
		string(@pval,"%9.3fThe")))))) ///
	name(ap_traits_change, replace) 
	graph export ap_traits_change.png, replace
	
	*In group
	reg ap_traits_ip_change  i.treatment
	estimates store ap_traits_ip_change
	margins, at(treatment=(1(1)3)) post
	coefplot, vertical ///
	ciopts(recast(rcap) msize(small)) ///
	scheme(plotplain) ///
	xlabel(1 `" "Plurality" "Comp."' 2 `" "RCV" "Comp."'  3 `" "RCV" "Comp." "& Endorse"' ) ///
	xscale(r(1.25 2.25)) ///
	title("", size(medium)) ///
	ytitle("In-Group Affect {&Delta} ") ///
	yscale(r(-.04 .04)) ///
	ylabel(-.03(.01).03) ///
	yline(0, lpattern(longdash) lcolor(black%60)) ///
	mlabel(cond(@pval>.05, string(@b,"%9.3f") + "", ///
		cond(@pval<.05, string(@b,"%9.3f") + "***", ///
		cond(@pval<.01, string(@b,"%9.3f") + "**", ///
		cond(@pval<.001, string(@b,"%9.3f") + "*", ///
		string(@pval,"%9.3fThe")))))) ///
	name(ap_traits_ip_change, replace) 
	graph export ap_traits_ip_change.png, replace
	
	*Out group
	reg ap_traits_op_change  i.treatment
	estimates store ap_traits_op_change
	margins, at(treatment=(1(1)3)) post
	coefplot, vertical ///
	ciopts(recast(rcap) msize(small)) ///
	scheme(plotplain) ///
	xlabel(1 `" "Plurality" "Comp."' 2 `" "RCV" "Comp."'  3 `" "RCV" "Comp." "& Endorse"' ) ///
	xscale(r(1.25 2.25)) ///
	title("", size(medium)) ///
	ytitle("Out-Group Affect {&Delta} ") ///
	yscale(r(-.04 .04)) ///
	ylabel(-.03(.01).03) ///
	yline(0, lpattern(longdash) lcolor(black%60)) ///
	mlabel(cond(@pval>.05, string(@b,"%9.3f") + "", ///
		cond(@pval<.05, string(@b,"%9.3f") + "***", ///
		cond(@pval<.01, string(@b,"%9.3f") + "**", ///
		cond(@pval<.001, string(@b,"%9.3f") + "*", ///
		string(@pval,"%9.3fThe")))))) ///
	name(ap_traits_op_change, replace) 
	graph export ap_traits_op_change.png, replace
	
	coefplot ap_traits_change ap_traits_ip_change ap_traits_op_change, ///
	drop(_cons) xline(0) ///
	xscale(r(-.015 .015)) ///
	xlabel(-.01(.01).01) ///
	scheme(plotplain) ///
	legend(order(2 "In- minus Out-Ideology" 4 "In-Ideology" 6 "Out-Ideology") row(1) pos(6)) ///
	title("Traits") ///
	mlabel(cond(@pval>.05, string(@b,"%9.3f") + "", ///
	cond(@pval<.001, string(@b,"%9.3f") + "***", ///
	cond(@pval<.01, string(@b,"%9.3f") + "**", ///
	cond(@pval<.05, string(@b,"%9.3f") + "*", ///
	string(@pval,"%9.3f")))))) ///
	mlabpos(12) ///
	mlabgap(2.5) ///
	name(coef_t, replace)

	graph combine ap_traits_change ap_traits_ip_change ap_traits_op_change, ///
	ycommon xcommon ///
	rows(1) ///
	name(traits_combined, replace)
	graph export traits_combined.png, replace 
	
	
	graph combine ap_ft_change ap_ft_ip_change ap_ft_op_change ///
	 ap_traits_change ap_traits_ip_change ap_traits_op_change, ///
	ycommon xcommon ///
	rows(2) ///
	name(both_combined, replace)
	graph export both_combined.png, replace 

*Feeling Thermometer table
	esttab ap_ft_change ap_ft_ip_change ap_ft_op_change ///
	using apft.tex, replace ///
	cells(b(star fmt(3)) se(fmt(3) par) ) starlevels( * 0.05 ** 0.01 *** 0.001) ///
	label ///
	stat(r2 N, fmt(2 0) label("R$^2$" "\textit{N}")) ///
	nodepvars nobase noomitted interaction(" x ") ///
	drop() ///
	style(tex) compress ///
	title("Electoral System, Candidate Communication, and Change in Partisan Affect (Feeling Thermometer)\label{tab:apft}") ///
	mlabel("Affective Polarization" "In-Group" "Out-Group") 
		
*Traits table
	esttab ap_traits_change ap_traits_ip_change ap_traits_op_change ///
	using apt.tex, replace ///
	cells(b(star fmt(3)) se(fmt(3) par) ) starlevels( * 0.05 ** 0.01 *** 0.001) ///
	label ///
	stat(r2 N, fmt(2 0) label("R$^2$" "\textit{N}")) ///
	nodepvars nobase noomitted interaction(" x ") ///
	drop() ///
	style(tex) compress ///
	title("Electoral System, Candidate Communication, and Change in Partisan Affect (Traits)\label{tab:apt}") ///
	mlabel("Affective Polarization" "In-Group" "Out-Group") 	
	
********************************************************************************	
********************************************************************************
********************************************************************************
**#Appendix: Summary Statistics, Balance Table, Pre-Power Analyses
********************************************************************************
********************************************************************************
********************************************************************************

*DV Correlations
pwcorr ap_ft_change ap_traits_change, sig
	
*Full Sample
estpost sum ap_ft_change ap_ft_ip_change ap_ft_op_change ap_traits_change ap_traits_ip_change ap_traits_op_change ///
age female educ white liberal conserv dem_wlean rep_wlean 
estimates store sumstat
esttab sumstat using sumstat.tex, ///
cells("mean(fmt(3)) sd(fmt(3)) min(fmt(3)) max(fmt(3)) count(fmt(0))") ///
collabels("Mean" "Std. Dev" "Min." "Max." "N") ///
label replace ///
nonumber noobs
	
*Libs
estpost sum ap_ft_change ap_ft_ip_change ap_ft_op_change ap_traits_change ap_traits_ip_change ap_traits_op_change ///
age female educ white liberal conserv dem_wlean rep_wlean if liberal ==1
estimates store sumstat_d
esttab sumstat_d using sumstat_d.tex, ///
cells("mean(fmt(3)) sd(fmt(3)) min(fmt(3)) max(fmt(3)) count(fmt(0))") ///
collabels("Mean" "Std. Dev" "Min." "Max." "N") ///
label replace ///
nonumber noobs

*Cons
estpost sum ap_ft_change ap_ft_ip_change ap_ft_op_change ap_traits_change ap_traits_ip_change ap_traits_op_change ///
age female educ white liberal conserv dem_wlean rep_wlean if conserv ==1
estimates store sumstat_r
esttab sumstat_r using sumstat_r.tex, ///
cells("mean(fmt(3)) sd(fmt(3)) min(fmt(3)) max(fmt(3)) count(fmt(0))") ///
collabels("Mean" "Std. Dev" "Min." "Max." "N") ///
label replace ///
nonumber noobs

iebaltab age female educ white liberal conserv dem_wlean rep_wlean, group(treatment) savet(bal) replace rowvarlabels nonote starlevels(.05 .01 .001)  addnote(*. .05 **.01 ***.001) control(1) total onerow

power oneslope 0 (.005 .0075 .01 .03 .05), alpha(.05) power(.8) sdy(.08) sdx(.8) ///
graph( ///
plotopts( ///
lcolor(black) ///
color(black) ///
yline(2250) ///
) ///
)
graph export power.png, replace 
